#include "playtoolbar.h"
#include "ui_playtoolbar.h"
#include <QTime>
#include "xh_avcodeccontroler.h"

playToolBar::playToolBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::playToolBar)
{
    ui->setupUi(this);

    ui->comboBox_speed->addItem("0.5", 0.5);
    ui->comboBox_speed->addItem("1.0", 1.0);
    ui->comboBox_speed->addItem("1.5", 1.5);
    ui->comboBox_speed->addItem("2.0", 2.0);
    ui->comboBox_speed->addItem("3.0", 3.0);
    ui->comboBox_speed->addItem("4.0", 4.0);

    ui->comboBox_speed->setCurrentIndex(1);

    void  (QComboBox::*currentIndexChanged)(int) = &QComboBox::currentIndexChanged;

    connect(ui->comboBox_speed, currentIndexChanged, this, [=](int index){
        //设置播放速度
        if (m_controler)
            m_controler->setSpeed(ui->comboBox_speed->currentData().toDouble());
    });
}

playToolBar::~playToolBar()
{
    delete ui;
}

void playToolBar::setDuration(uint64_t duration)
{
    m_duration = duration;
    ui->label_totalTime->setText(convertSecond(m_duration));
    ui->horizontalSlider->setMaximum(m_duration);
}

void playToolBar::setCurrentTimeStamp(uint64_t timeStamp)
{
    if (m_isPressed)
        return;

    m_timeStamp = timeStamp;
    ui->label_currentTime->setText(convertSecond(m_timeStamp));
    ui->horizontalSlider->setValue(m_timeStamp);
}

void playToolBar::setAVCodecControler(xh_AVCodecControler *controler)
{
    m_controler = controler;
    if (m_controler) {
        m_controler->setMuted(ui->pushButton_volume->isChecked());
    }

    ui->pushButton_fullScreen->setChecked(true);
    ui->pushButton_play->setChecked(true);
}

bool playToolBar::isMuted()
{
    return ui->pushButton_volume->isChecked();
}

QString playToolBar::convertSecond(uint64_t second)
{
    QTime time(0, 0, 0);
    time = time.addSecs(second);
    return time.toString("hh:mm:ss");
}

void playToolBar::on_pushButton_play_clicked(bool checked)
{
    if (!m_controler)
        return;
    if (checked)
        m_controler->SetMediaStatusPlay();
    else
        m_controler->SetMediaStatusPause();
}

void playToolBar::on_horizontalSlider_sliderPressed()
{
    m_isPressed = true;
}

void playToolBar::on_horizontalSlider_sliderReleased()
{
    m_isPressed = false;

    if (!m_controler)
        return;

    m_controler->seekMedia(m_duration * ui->horizontalSlider->value() / ui->horizontalSlider->maximum(), VIDEO);
}

void playToolBar::on_pushButton_volume_clicked(bool checked)
{
    if (!m_controler)
        return;
    m_controler->setMuted(ui->pushButton_volume->isChecked());
}

void playToolBar::on_pushButton_fullScreen_clicked(bool checked)
{
    emit fullScreen(!checked);
}
